<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CommentController
 *
 * @author Hoan Vu
 */
class User_CommentController extends Zend_Controller_Action {
    function indexAction() {
        $commentDao = new CommentDAO();
        $this->view->commentList = $commentDao->getCommentList();
    }

    function addAction() {
        $commentDao = new CommentDAO();

        $userSession = new Zend_Session_Namespace('user');

        if ($this->_request->isPost()) {
            $comment = $this->_request->getParam('comment');
            $product_id = $this->_request->getParam('id');
            $user_id = $userSession->user_id;
            $data = array(
                'comment' => $comment,
                'product_id' => $product_id,
                'user_id' => $user_id,
                'status' => Constant::$STATUS_AVAILABLE,
            );
            $commentDao->addComment($data);
            $this->_redirect("/index/view/id/".$product_id);
        }
    }

    function updateAction() {
        $commentDao = new CommentDAO();

        if ($this->_request->isPost()) {
            $id = $this->_request->getParam('id');
            $comment = $this->_request->getParam('comment');
            $product_id = $this->_request->getParam('product_id');
            $data = array(
                'id' => $id,
                'comment' => $comment,
                'product_id' => $product_id,
                'user_id' => $user_id,
                'status' => Constant::$STATUS_AVAILABLE,
            );
            $roleDao->editRole($data);

            $this->_redirect('/admin/comment');
        } else {
            $id = (int)$this->_request->getParam('id', 0);

            if ($id > 0) {
                $this->view->oldComment = $commentDao->getComment($id);
            }
        }
    }

    function deleteAction() {
        $commentDao = new CommentDAO();
        $id = $this->_request->getParam('id');
        $commentDao->deleteComment($id);
        $this->_redirect('admin/comment');
    }
}